package com.ruoyi.wx;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.wx.annotation.AppUser;
import com.ruoyi.common.wx.domain.WxUser;
import com.ruoyi.common.wx.domain.vo.WxUserRegisterVo;
import com.ruoyi.common.wx.domain.vo.WxVo;
import com.ruoyi.common.wx.service.IWxUserService;
import com.ruoyi.common.wx.service.IWxService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@AllArgsConstructor
@RestController
@RequestMapping({"/app/user"})
public class WxUserController {
    @Autowired
    IWxUserService wxUserService;

    @Autowired
    IWxService wxService;

    /**
     * <pre>
     *     用户登录，getWxInfo接口先通过openID查询数据库是否有用户数据，有就直接返回，如果没有loginOrRegister接口就授权获取手机号码注册用户
     * 微信授权获取openId,实现登录或注册
     * </pre>
     */
    @PostMapping("/loginOrRegisterByOpenId")
    public AjaxResult loginOrRegisterByOpenId(@RequestBody WxVo input) {
        try {
            return AjaxResult.success(wxUserService.loginOrRegisterByOpenId(input.getJsCode()));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * <pre>
     * 微信授权获取openId
     * </pre>
     */
    @PostMapping("/getWxInfo")
    public AjaxResult getWxInfo(@RequestBody WxVo input) {
        try {
            return AjaxResult.success(wxService.getWxInfo(input.getJsCode()));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 微信授权获取手机号
     */
    @PostMapping("/getWxPhone")
    public AjaxResult getWxPhone(@RequestBody WxUserRegisterVo input) {
        try {
            return AjaxResult.success().put("data", wxUserService.getWxPhone(input));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 登录 或者 注册 用户
     */
    @PostMapping("/loginOrRegister")
    public AjaxResult add(@RequestBody WxUserRegisterVo input) {
        try {
            String tokenRes = wxUserService.loginOrRegister(input);
            if (Constants.USER_NOT_EXIST.equals(tokenRes)) {
                return AjaxResult.error(Constants.USER_NOT_EXIST);
            } else {
                HashMap token = new HashMap<String, Object>(1) {{
                    put("token", tokenRes);
                }};
                return AjaxResult.success().put("data", token);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 延续token有效时间
     */
    @GetMapping("/refreshToken")
    public AjaxResult refreshToken() {
        try {
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取登录人信息
     */
    @GetMapping("/info")
    public AjaxResult info(@AppUser WxUser input) {
        try {
            return AjaxResult.success(input);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }


}
